import { createRouter, createWebHashHistory } from 'vue-router'
import MainLayout from '../views/Layout/MainLayout.vue' // Main(主要区域容器)布局组件

// 1.声明路由 2.注册路由 3.导出路由
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: 'Login' },
    component: () => import('../views/Login.vue')
  },
  {
    path: '/layout',
    name: 'Layout',
    meta: { browserTitle: '藤田MES', title: '首页' },
    component: () => import('../views/Layout/Layout'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: { title: '首页' },
        component: () => import('../views/Layout/Home/index.vue')
      },
      {
        path: '/command',
        name: 'Command',
        meta: { title: '指令管理' },
        component: () => import('../views/Layout/Command/index.vue')
      },
      {
        path: '/systemManage',
        name: 'SystemManage',
        meta: { title: '系统管理' },
        component: MainLayout,
        children: [
          {
            path: '/userList',
            name: 'UserList',
            meta: { title: '用户管理' },
            component: () => import('../views/Layout/System/UserManagement/index.vue')
          },
          {
            path: '/role',
            name: 'role',
            meta: { title: '角色管理' },
            component: () => import('../views/Layout/System/RoleManagement/index.vue')
          },
          {
            path: '/permission',
            name: 'permission',
            meta: { title: '权限管理' },
            component: () => import('../views/Layout/System/MenuManagement/index.vue')
          },
          {
            path: '/params',
            name: 'Params',
            meta: { title: '系统参数管理' },
            component: () => import('../views/Layout/System/SysParamsManagement/index.vue')
          }
        ]
      },
      {
        path: '/monitoring',
        name: 'Monitoring',
        meta: { title: '系统监控' },
        component: MainLayout
      },
      {
        path: '/planManage',
        name: 'PlanManage',
        meta: { title: '计划管理' },
        component: MainLayout,
        children: [
          {
            path: '/orderList',
            name: 'OrderList',
            meta: { title: '订单管理' },
            component: () => import('../views/Layout/plan/OrderManagement/index.vue')
          },
          {
            path: '/productPlan',
            name: 'ProductPlan',
            meta: { title: '生产计划' },
            component: () => import('../views/Layout/plan/ProductPlan/index.vue')
          }
        ]
      },
      {
        path: '/craft',
        name: 'Craft',
        meta: { title: '工艺管理' },
        component: MainLayout,
        children: [
          {
            path: '/processList',
            name: 'ProcessList',
            meta: { title: '工序管理' },
            component: () => import('../views/Layout/craft/ProcedureManagement/index.vue')
          },
          {
            path: '/craftList',
            name: 'CraftList',
            meta: { title: '工艺路线管理' },
            component: () => import('../views/Layout/craft/ProcessRouteManagement/index.vue')
          }
        ]
      }
    ]
  },
  {
    path: '/:catchAll(.*)',
    name: '404',
    meta: { title: '404页面' },
    component: () => import('../views/404.vue')
  }
]

// 注册路由
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

/**
 * 导航守卫,使用router.beforeEach注册一个全局前置守卫，判断用户是否登陆
 * to 将要访问的路径 from 代表从哪个路径跳转而来 next是一个函数，表示放行 next('/login') 强制跳转
 */
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = localStorage.getItem('Authorization')
    if (token === null || token === '') {
      next('/login')
    } else {
      next()
    }
  }
})

// 导出路由
export default router
